<?php
/*业务处理基本类*/
include_once('config.php');
include_once(config::getCommPath()."tool.class.php");
class bll
{
	public $db,$objData,$request_data,$InfoKind,$sql,$keyFld,$subFld,$subListNm,$err;

	public function __construct($request_data){
        $data=file_get_contents("php://input");
        $this->JSONLog($data);
		$this->objData=json_decode($data);
        if (property_exists($this->objData,'ckey')){
            //安全性检查
            $md5Key=trim($this->objData->ckey);
            $DateMd5=md5(date('Ymd'));
            //if ($md5Key!=$DateMd5){
            //    $this->err='非法用户！';
            //}
            //else{
                //初始设置
                $keys=array_keys($_REQUEST);
                $this->InfoKind=$keys[0];
                $this->db=config::getDB('sqlsvr');
                $this->request_data=$request_data;
                tool::$logsPath= config::getLogPath();
            //}
        }else
	    	$this->err='无效调用！';
	}

    //获取数据
	public function getData()
    {
    	if ($this->err)
    		return tool::PHPInfo('',false,$this->err);
    	else
	        try{
	            return $this->getERPData();
	        }catch(Exception $e) {
	            tool::logs('后台取数错误：'.$this->sql.','. $e -> getMessage());
	            return tool::PHPInfo('',false,'后台取数错误');
	        }
    }

    //获取ERP数据
	public function getERPData()
    {
        $this->sql='';
        $cCallId='';
        $mainSql='';
		$subSql='';
		$iRn=1;
        foreach ($this->objData as $key => $value) {
            if ($key=='ckey')
                ;
            elseif ($key=='cCallerId')
            	$cCallerId = $value;
			elseif ($key=='data'){
	            	foreach ($value as $dr) {
		            	$c1='';
		            	foreach ($dr as $subkey => $subval) {
			                $c1.=',"'.$subval.'"';
		            	}
		            	$subSql.='|'.substr($c1,1);
		            	if (in_array($this->InfoKind, config::$RnoTbl)){
		            		$subSql.=",$iRn";
		            		$iRn+=1;
		            	}						
	            	}
    			}
	            else{
	                $mainSql.=',"'.$value.'"';
	        }
        }
		$mainSql=substr($mainSql,1);
		$subSql=substr($subSql,1);
		$this->sql='usp_xmy_'.$this->InfoKind.' \''.$cCallerId.'\',\''.$subSql.'\'';
        //log
        if (config::isDebug())
            tool::logs('BLL Call:'.$this->sql);
       	//
        $data=$this->db->DB_Proc($this->sql);
        if (substr($data,0,5)=='error')
        	return tool::PHPInfo($data,false);
        elseif ($data){
	        $Recc=0;
	        if ($data &&  count($data)>0){
	        	$keys=array_keys($data[0]);
	        	if ($keys[0]=='pageIndex'){
	        		$Recc=$data[0]['iRecc'];
	        		$res=array();
	        		foreach ($data as $dr) {
	        			$drArr=array();
	        			foreach ($dr as $key => $value) {
	        				if ($key=='pageIndex' || $key=='iRecc')
	        					;
	        				else
	        					$drArr[$key]=$value;
	        			}
		        		$res[]=$drArr;
	        		}
	        		$data=$res;
	        		return tool::PHPInfo($data,true,'',$Recc);
	        	}
	        	elseif ($keys[0]=='cRes'){
	        		$cRes=$data[0]['cRes'];	
	        		return tool::PHPInfo('',substr($cRes,0,1)=='1',substr($cRes,1),0);
	        	}
	        }
	       	return tool::PHPInfo($data,true,'',$Recc);
		}else
            return tool::PHPInfo('',true,'',0);
    } 

    //传入的json语句进行备份
    public function JSONLog($Info){
    	$Path='JSONLog';
    	if (!is_dir($Path))
    		mkdir($Path, 0777);
		return file_put_contents($Path.'//JSON'.date("Ym",time()).".log", date("Ymd H:i",time()).' :'.$Info."\r\n",FILE_APPEND);
    }
}